package org.dromara.product.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.product.domain.bo.KyStockOutRecordsBo;
import org.dromara.product.domain.vo.KyStockOutRecordsVo;
import org.dromara.product.domain.KyStockOutRecords;
import org.dromara.product.mapper.KyStockOutRecordsMapper;
import org.dromara.product.service.IKyStockOutRecordsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 出库记录Service业务层处理
 *
 * @author kuyu
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class KyStockOutRecordsServiceImpl implements IKyStockOutRecordsService {

    private final KyStockOutRecordsMapper baseMapper;

    /**
     * 查询出库记录
     */
    @Override
    public KyStockOutRecordsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询出库记录列表
     */
    @Override
    public TableDataInfo<KyStockOutRecordsVo> queryPageList(KyStockOutRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyStockOutRecords> lqw = buildQueryWrapper(bo);
        Page<KyStockOutRecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出库记录列表
     */
    @Override
    public List<KyStockOutRecordsVo> queryList(KyStockOutRecordsBo bo) {
        LambdaQueryWrapper<KyStockOutRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyStockOutRecords> buildQueryWrapper(KyStockOutRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyStockOutRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMid() != null, KyStockOutRecords::getMid, bo.getMid());
        lqw.eq(StringUtils.isNotBlank(bo.getStockNo()), KyStockOutRecords::getStockNo, bo.getStockNo());
        lqw.eq(bo.getWarehouseId() != null, KyStockOutRecords::getWarehouseId, bo.getWarehouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatform()), KyStockOutRecords::getPlatform, bo.getPlatform());
        lqw.eq(bo.getOutTypeId() != null, KyStockOutRecords::getOutTypeId, bo.getOutTypeId());
        lqw.eq(bo.getCustomerId() != null, KyStockOutRecords::getCustomerId, bo.getCustomerId());
        lqw.eq(StringUtils.isNotBlank(bo.getImages()), KyStockOutRecords::getImages, bo.getImages());
        lqw.eq(bo.getTransInId() != null, KyStockOutRecords::getTransInId, bo.getTransInId());
        lqw.eq(bo.getStatus() != null, KyStockOutRecords::getStatus, bo.getStatus());
        lqw.eq(bo.getApproveLevel() != null, KyStockOutRecords::getApproveLevel, bo.getApproveLevel());
        lqw.eq(bo.getPrintNum() != null, KyStockOutRecords::getPrintNum, bo.getPrintNum());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordInfo()), KyStockOutRecords::getRecordInfo, bo.getRecordInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), KyStockOutRecords::getMark, bo.getMark());
        lqw.eq(bo.getDeletedAt() != null, KyStockOutRecords::getDeletedAt, bo.getDeletedAt());
        lqw.eq(StringUtils.isNotBlank(bo.getCustom()), KyStockOutRecords::getCustom, bo.getCustom());
        lqw.eq(bo.getTransId() != null, KyStockOutRecords::getTransId, bo.getTransId());
        lqw.eq(bo.getFromWarehouseId() != null, KyStockOutRecords::getFromWarehouseId, bo.getFromWarehouseId());
        lqw.eq(bo.getFromZoneId() != null, KyStockOutRecords::getFromZoneId, bo.getFromZoneId());
        return lqw;
    }

    /**
     * 新增出库记录
     */
    @Override
    public Boolean insertByBo(KyStockOutRecordsBo bo) {
        KyStockOutRecords add = MapstructUtils.convert(bo, KyStockOutRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改出库记录
     */
    @Override
    public Boolean updateByBo(KyStockOutRecordsBo bo) {
        KyStockOutRecords update = MapstructUtils.convert(bo, KyStockOutRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyStockOutRecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出库记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
